package com.mulanweb.handler.wechat;

import com.jfinal.kit.StrKit;
import com.mulanweb.api.module.model.*;
import com.mulanweb.db.common.Constant;
import com.mulanweb.db.common.RandomStringGenerator;
import com.mulanweb.db.common.WxParaFormatter;
import com.mulanweb.utils.AseUtils;
import com.mulanweb.utils.RandomUtil;
import com.mulanweb.utils.WxSignUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Created by Administrator on 2016/11/17 0017.
 */
public class CheckOrderPayStatus {

    /**
     * 查询用户是否支付
     * @param orderNumber 商户订单号
     * @return 布尔结果类型
     */
    public static Boolean checkPayStatus(String orderNumber){
        String appid = Constant.WeChatFinalValue.APPID;//微信分配的公众账号ID（企业号corpid即为此appId）
        String mch_id = Constant.WeChatFinalValue.MCHID;//微信支付分配的商户号
        String nonce_str = RandomStringGenerator.getRandomStringByLength(32);//这是随机字符串
        String sign = null;
        SortedMap<Object, Object> para = new TreeMap<Object, Object>();
        para.put("appid", appid);
        para.put("mch_id", mch_id);
        para.put("nonce_str", nonce_str);
        para.put("out_trade_no",orderNumber);
        sign = WxSignUtil.createGetPrepayIdSign(para);//进行微信特定签名
        para.put("sign", sign);
        String xml = WxParaFormatter.getRequestXml(para);//将所有参数转换成xml格式

        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(Constant.WeChatFinalValue.QUERY_ORDER_URL);
        post.addHeader("Content/Type", "application/xml");
        StringEntity stringEntity = new StringEntity(xml, ContentType.create("application/xml", "UTF-8"));
        post.setEntity(stringEntity);
        CloseableHttpResponse resp = null;
        try {
            resp = client.execute(post);
        } catch (IOException e) {
            e.printStackTrace();
        }
        int returnCode = resp.getStatusLine().getStatusCode();
        String str = "";
        if (200 <= returnCode && 300 >= returnCode) {
            try {
                str = (EntityUtils.toString(resp.getEntity()));

            Map<String, String> result = WxParaFormatter.xmlToMap(str);
                System.out.println(result);
                if(result.containsKey("trade_state") && "SUCCESS".equals(result.get("trade_state"))){
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

}
